package de.programmerspain.rv3sf.api;
/**
 * enum class used to define the return states of the Rainbowduino V3 controller.
 * 
 * @author Markus Lang (m@rkus-lang.de) | http://programmers-pain.de/ | https://code.google.com/p/rainbowduino-v3-streaming-firmware/
 */
public enum RainbowduinoV3StateEnum {
	/**
	 * normal return state if sending of the frame was successful.
	 */
	STATE_ACK((byte) 0x30, false),
	/**
	 * error state if the controller wasn't able to parse the frame fragment index.
	 */
	STATE_FRAME_FRAGMENT_INDEX((byte) 0x31, true),
	/**
	 * error state in case a frame fragment was send in the wrong order.
	 */
	STATE_INCOMPLETE_FRAME((byte) 0x32, true);
	private byte code;
	private boolean error;
	
	/**
	 * @param code the byte code of the state
	 * @param error <code>true</code> if this state should be handled as an error.
	 */
	private RainbowduinoV3StateEnum(byte code, boolean error) {
		this.code = code;
		this.error = error;
	}
	
	/**
	 * returns the matching enum instance for the given error code
	 * 
	 * @param code the byte error code
	 * @return the matching enum instance or <code>null</code>
	 */
	public static RainbowduinoV3StateEnum getErrorForCode(byte code) {
		for (RainbowduinoV3StateEnum rainbowduinoV3ErrorEnum : RainbowduinoV3StateEnum.values()) {
			if (rainbowduinoV3ErrorEnum.getCode() == code) {
				return rainbowduinoV3ErrorEnum;
			}
		}
		return null;
	}

	/**
	 * @return the byte code
	 */
	public byte getCode() {
		return this.code;
	}
	
	/**
	 * @return <code>true</code> if current enum instance is an error state
	 */
	public boolean isError() {
		return this.error;
	}
}
